package Admin;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;

public class Style extends JPanel {
	JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	private JTextField s_code;
	private JTextField s_item;
	private JTextField s_category;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JButton button;
	ManageDTO dto;
	private JLabel label_3;
	
	
	
	/**
	 * Create the panel.
	 */
	public Style(JPanel contentPane) {
		setBackground(Color.WHITE);
		this.contentPane=contentPane;
		setLayout(null);
		add(getTextField());
		add(getBtnNewButton());
		add(getScrollPane());
		add(getS_code());
		add(getS_item());
		add(getS_category());
		add(getLabel());
		add(getLabel_1());
		add(getLabel_2());
		add(getBtnNewButton_1());
		add(getBtnNewButton_2());
		add(getButton());
		add(getLabel_3());
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(174, 86, 132, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					select();
					
					
					
					
				}
			});
			btnNewButton.setBounds(318, 85, 97, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(174, 128, 240, 390);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					int r = table.getSelectedRow();
					s_code.setText(""+table.getValueAt(r, 0));
					s_item.setText((String)table.getValueAt(r, 1));
					s_category.setText((String)table.getValueAt(r, 2));
										
				}
			});
		}
		return table;
	}
	private JTextField getS_code() {
		if (s_code == null) {
			s_code = new JTextField();
			s_code.setEditable(false);
			s_code.setBounds(500, 159, 139, 23);
			s_code.setColumns(10);
		}
		return s_code;
	}
	private JTextField getS_item() {
		if (s_item == null) {
			s_item = new JTextField();
			s_item.setColumns(10);
			s_item.setBounds(500, 188, 139, 23);
		}
		return s_item;
	}
	private JTextField getS_category() {
		if (s_category == null) {
			s_category = new JTextField();
			s_category.setColumns(10);
			s_category.setBounds(500, 217, 139, 23);
		}
		return s_category;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드번호");
			label.setFont(new Font("Gulim", Font.PLAIN, 12));
			label.setBounds(428, 159, 62, 21);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("스타일이름");
			label_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_1.setBounds(426, 189, 75, 21);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("카테고리");
			label_2.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_2.setBounds(426, 216, 62, 21);
		}
		return label_2;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("등록");
			btnNewButton_1.setBackground(Color.WHITE);
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dto = new ManageDTO();
					
					HairStyleVo v = new HairStyleVo();
					
					v.setItem(s_item.getText());
					v.setCategory(s_category.getText());					
					
					if(dto.inserthair(v)){
						JOptionPane.showMessageDialog(Style.this, "등록 성공");
						select();
					}else{
						JOptionPane.showMessageDialog(Style.this, "등록 오류");
					}
					
					
				}
			});
			btnNewButton_1.setBounds(542, 250, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("수정");
			btnNewButton_2.setBackground(Color.WHITE);
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dto = new ManageDTO();
					
					HairStyleVo v = new HairStyleVo();
					v.setCode(Integer.parseInt(s_code.getText()));
					v.setItem(s_item.getText());
					v.setCategory(s_category.getText());	
					
					
					if(dto.updatehair(v)){
						JOptionPane.showMessageDialog(Style.this, "수정 성공");
						select();
					}else{
						JOptionPane.showMessageDialog(Style.this, "수정 오류");
					}
					
				}
			});
			btnNewButton_2.setBounds(542, 283, 97, 23);
		}
		return btnNewButton_2;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.setBackground(Color.WHITE);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dto = new ManageDTO();
					
					int code = Integer.parseInt(s_code.getText());
					
					if(dto.deletehair(code)){
						JOptionPane.showMessageDialog(Style.this, "삭제 성공");
						select();
					}else{
						JOptionPane.showMessageDialog(Style.this, "삭제 오류");
					}
				}
			});
			button.setBounds(542, 317, 97, 23);
		}
		return button;
	}
	
	public void select(){
		Vector<String> v = new Vector<String>();
		v.add("CODE");
		v.add("헤어스타일");
		v.add("Category");
		
		
		DefaultTableModel tmodel = new DefaultTableModel(v,0);
		
		dto = new ManageDTO();
		
		ArrayList<HairStyleVo> al =	dto.selecthair(textField.getText());
		
		for(HairStyleVo vo : al){
			Vector vec = new Vector();
			vec.add(vo.getCode());
			vec.add(vo.getItem());
			vec.add(vo.getCategory());
			
			tmodel.addRow(vec);
		}
		
		table.setModel(tmodel);
		
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("스타일 관리");
			label_3.setFont(new Font("굴림", Font.BOLD, 23));
			label_3.setBounds(174, 10, 232, 52);
		}
		return label_3;
	}
}

